package com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ProjectMapper;
import com.pojo.Project;
import com.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;


    @Override
    public PageInfo<Project> selectAllProjects(Map map) {
        int pageNum = (int)map.get("pageNum");
        PageHelper.startPage(pageNum,8);
        List<Project> projects = projectMapper.selectAllProjects(map);
        PageInfo pageInfo=new PageInfo(projects);
        return pageInfo;
    }

    @Override
    public int insertProject(Project project) {
        //当前时间
        Date date=new Date();
        project.setCreatedate(date);
        long day = project.getDay();
        //完成时间
        Date date1 = addDayOfDate(date, day);
        project.setDeploydate(date1);
        return projectMapper.insertProject(project);
    }

    @Override
    public int updateProject(Project project) {
        Date date=new Date();
        project.setCreatedate(date);
        return projectMapper.updateProject(project);
    }

    @Override
    public int deleteProject(long id) {
        return projectMapper.deleteProject(id);
    }

    @Override
    public Project selectProjectById(long id) {
        return projectMapper.selectProjectById(id);
    }

    @Override
    public PageInfo<Project> selectProjectByuserId(int pageNum,long userId,String status) {
        PageHelper.startPage(pageNum,3);
        List<Project> projects = projectMapper.selectProjectByuserId(userId, status);
        PageInfo<Project> pageInfo = new PageInfo<>(projects);
        return pageInfo;
    }

    @Override
    public int startProject(Project project) {
        project.setDeploydate(new Date());
        return projectMapper.startProject(project);
    }

    @Override
    public int updateStartProject(Project project) {
        project.setDeploydate(new Date());
        return projectMapper.updateStartProject(project);
    }

    @Override
    public List<Project> selectProjects() {
        return projectMapper.selectProjects();
    }

    @Override
    public Project selectProjectByProjectName(String projectName) {
        return projectMapper.selectProjectByProjectName(projectName);
    }

    @Override
    public int updataProjectEnd(Project project) {
        return projectMapper.updataProjectEnd(project);
    }

    /**
      * 在原日期的基础上增加天数
      * @param date
      * @param i
      * @return
      */
        public static Date addDayOfDate(Date date,long i){
          Calendar c = Calendar.getInstance();
          c.setTime(date);
          c.add(Calendar.DATE, (int) i);
          Date newDate = c.getTime();
          return newDate;
        }
}
